#include "StdAfx.h"
#include "TPGradientOutLinePloygon.h"
#include "math.h"
CTPGradientOutLinePloygon::CTPGradientOutLinePloygon(void)
{
}

CTPGradientOutLinePloygon::~CTPGradientOutLinePloygon(void)
{
}

BOOL CTPGradientOutLinePloygon::FillColor( LPVOID pColorData, BYTE* & pBuffer, int nWidth, int nHeight, eBufferFormat eFormat )
{
	return FillInCMethod(pColorData,pBuffer,nWidth,nHeight,eFormat);
}

int CTPGradientOutLinePloygon::SaveColorToBuffer( LPVOID pColorDef, LPVOID lpBuffer, int nLength )
{
	return 0;
}

int CTPGradientOutLinePloygon::LoadColorFromBuffer( LPVOID hColor,LPVOID lpBuffer,int nLength )
{
	return 0;
}

BOOL CTPGradientOutLinePloygon::FillInCMethod( LPVOID pColorData, BYTE* & pBuffer, int nWidth, int nHeight, eBufferFormat eFormat )
{
	TPCircleCenterGradientColor* pColorInfo = (TPCircleCenterGradientColor*)pColorData;
	int nTableSize = (int)(sqrt((float)(nWidth* nWidth + nHeight*nHeight)) * PI);
	int nNodeCount = pColorInfo->nNodeCount;
	TPColorValue* pTable = new TPColorValue[nTableSize];
	TPGradientNode* pNewNodeList = NULL;
	InitCircleNodeList(pNewNodeList,pColorInfo->pGradientNode,pColorInfo->nNodeCount);
		
	InitColorTable(nTableSize, pTable, pNewNodeList, pColorInfo->nNodeCount+1, 1, 0);
	delete[] pNewNodeList;
	

	BYTE* pCurrent = pBuffer;
	int nHalfHeight = nHeight / 2;
	float fCenterX = float(nWidth / 2);
	float fCenterY = float(nHeight/ 2);
	float fTheta = 0;
	float fPos = 0;

	for (int i=0;i<nHeight;i++)
	{
		for (int j=0;j<nWidth;j++)
		{
			fTheta = CalcTheta(i,j,fCenterX,fCenterY);

			if (i>=(float)(nHeight/2))
			{
				fTheta = float(2*PI - fTheta);
			}
			int nPos = (int)(fTheta* nTableSize / (2 * PI));
			pCurrent[0] = TPGetRValue(pTable[nPos]);
			pCurrent[1] = TPGetGValue(pTable[nPos]);
			pCurrent[2] = TPGetBValue(pTable[nPos]);
			pCurrent[3] = TPGetAValue(pTable[nPos]);
			pCurrent+=4;
		}
	}

	return TRUE;
}

float CTPGradientOutLinePloygon::CalcTheta( int i, int j, float fCenterX, float fCenterY )
{
	float fLength1 = sqrt(float((j+0.5f-fCenterX) * (j+0.5f-fCenterX) + (i+0.5-fCenterY)*(i+0.5-fCenterY)));
	float fDotRes = -(j+0.5f-fCenterX);
	float fTheta = acos(fDotRes / fLength1);
	return fTheta;
}

BOOL CTPGradientOutLinePloygon::InitCircleNodeList( TPGradientNode* & pNewNode, TPGradientNode* pNode, int nNodeCount )
{
	pNewNode = new TPGradientNode[nNodeCount + 1];
	for (int i=0;i<nNodeCount;i++)
	{
		pNewNode[i].color = pNode[i].color;
		pNewNode[i].mid = pNode[i].mid;
		pNewNode[i].position = pNode[i].position;
	}
	pNewNode[nNodeCount].color = pNode[0].color;
	pNewNode[nNodeCount].mid = pNode[0].mid;
	pNewNode[nNodeCount].position = pNode[0].position;

	for (int i=0;i<=nNodeCount;i++)
	{
		pNewNode[i].position = float(i) / nNodeCount;
	}
 	for (int i=1;i<=nNodeCount;i++)
 	{
 		//pNewNode[i].mid = pNewNode[i].mid * (pNewNode[i].position - pNewNode[i-1].position);
		pNewNode[i].mid = 0.5f;

 	}
	
	return TRUE;
}
